



import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectTest {

    //创建对象
    public static void reflectNewInstance() {


        Class<?> c1=null;
        //1、获得class的对象的student类
        try {
            c1=Class.forName("操作练习.Student");//用的最多的

            //获得了student的对象
            //相当于Student student=new Student();
            //不带参数的构造方法
            Student student=(Student)c1.newInstance();//public T newInstance()   T类型所以强制转换
            System.out.println(student);



        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    // 反射私有的构造方法 屏蔽内容为获得公有的构造方法
    public static void reflectPrivateConstructor() {
        Class<?> c1=null;
        //1、获得class的对象的student类
        try {
            c1=Class.forName("操作练习.Student");//用的最多的

            //获取private方法权限
            Constructor<?> constructor=c1.getDeclaredConstructor(String.class,int.class);
            constructor.setAccessible(true);//开权限

            Student student=(Student)constructor.newInstance("yuanye",20);
            System.out.println(student);

        } catch (ClassNotFoundException | NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }



    // 反射私有属性
    public static void reflectPrivateField() {
        Class<?> c1=null;
        //1、获得class的对象的student类
        try {
            c1=Class.forName("操作练习.Student");//用的最多的
            Field field=c1.getDeclaredField("name");
            field.setAccessible(true);

            Student student=(Student) c1.newInstance();
            field.set(student,"yuanye");
            //System.out.println(student);

            //获取对象的值
            String str=(String)field.get(student);
            System.out.println(str);


        } catch (ClassNotFoundException | NoSuchFieldException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }


    // 反射私有方法
    public static void reflectPrivateMethod() {
        Class<?> c1=null;
        //1、获得class的对象的student类
        try {
            c1=Class.forName("操作练习.Student");//用的最多的

            Method method=c1.getDeclaredMethod("function",String.class);
            method.setAccessible(true);

            Student student=(Student)c1.newInstance();

            method.invoke(student,"给func函数传的参数");


        } catch (ClassNotFoundException | NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }



    public static void main(String[] args) {
        //reflectNewInstance();
        //reflectPrivateConstructor();
        //reflectPrivateField();
        reflectPrivateMethod();
    }

}

